﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Ryder_US_Website.BUS;

namespace Ryder_US_Website.Presentation.Admin
{
    public partial class ServiceDetail_List : System.Web.UI.Page
    { 
        private String []arr_trash = new String[2]{"Còn sử dụng","Đã xóa"};
        List<RUS_ServiceDetail> list = new List<RUS_ServiceDetail>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["RUS_User"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            list = ServiceDetailBUS.GetAll();
            DataTable dt = new DataTable();
            dt.Columns.Add("#");
            dt.Columns.Add("Tên chi tiết dịch vụ");
            dt.Columns.Add("Tên dịch vụ");
            //dt.Columns.Add("Đơn vị tính");
            //dt.Columns.Add("Trọng lượng tối thiểu");
            //dt.Columns.Add("Trọng lượng tối đa");
            dt.Columns.Add("Đơn giá");
            dt.Columns.Add("Ghi chú");
            dt.Columns.Add("Tình trạng");
            DataRow row = null;
            for(int i=0;i<list.Count;i++)
            {
                RUS_ServiceDetail kq = new RUS_ServiceDetail();
                row = dt.NewRow();
                row["#"] = (i + 1).ToString();
                row["Tên chi tiết dịch vụ"]="<a href='ServiceDetail_Update.aspx?id="+list[i].ServiceDetail_ID +"'>"+list[i].ServiceDetail_name + "</a>";
                row["Tên dịch vụ"] = ServiceBUS.GetActiveServiceByID(list[i].ServiceDetail_ServiceID).Service_name;
                //row["Đơn vị tính"] = list[i].ServiceDetail_unit;
                //row["Trọng lượng tối thiểu"] = list[i].ServiceDetail_unitMin;
                //row["Trọng lượng tối đa"] = list[i].ServiceDetail_unitMax;
                row["Đơn giá"] = list[i].ServiceDetail_price.ToString("F0", CultureInfo.InvariantCulture);
                row["Ghi chú"] = list[i].ServiceDetail_description;
                //row["Tình trạng"] = arr_trash[list[i].ServiceDetail_trash];
                dt.Rows.Add(row);
            }
            grvServiceDetail.DataSource = dt;
            grvServiceDetail.AllowPaging = true;
            grvServiceDetail.PageSize = 10;
            grvServiceDetail.DataBind();
        }

        protected void grvServiceDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType==DataControlRowType.DataRow)
            {
                int i = e.Row.RowIndex;
                e.Row.Cells[1].Text = Server.HtmlDecode(e.Row.Cells[1].Text);

                //Link button xóa
                LinkButton lkbtn = new LinkButton();
                lkbtn.Text = arr_trash[list[i].ServiceDetail_trash];
                lkbtn.Command += new CommandEventHandler(lkbtnXoa_Click);
                lkbtn.CommandArgument = list[i].ServiceDetail_ID.ToString();
                e.Row.Cells[5].Controls.Add(lkbtn);
            }
        }

        protected void grvServiceDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvServiceDetail.PageIndex = e.NewPageIndex;
            grvServiceDetail.DataBind();
        }

        protected void lkbtnXoa_Click(object sender, CommandEventArgs e)
        {
            try
            {
                ServiceDetailBUS.DeleteRestore(int.Parse(e.CommandArgument.ToString()));
            }
            catch (Exception ex)
            {
                Response.Write(Utils.UtilHelpers.ShowMessage(ex.Message));
            }

            Page_Load(sender, e);
        }
    }
}
